package cn.edu.neusoft.neufooddemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.neufooddemo.R;
import cn.edu.neusoft.neufooddemo.activity.FoodInfoActivity;
import cn.edu.neusoft.neufooddemo.activity.FoodListActivity;
import cn.edu.neusoft.neufooddemo.bean.CollectInfo;
import cn.edu.neusoft.neufooddemo.bean.Result;
import cn.edu.neusoft.neufooddemo.common.Constants;
import cn.edu.neusoft.neufooddemo.listener.RetrofitListener;
import cn.edu.neusoft.neufooddemo.model.UserModel;

/**
 * Created by Shinelon on 2017/3/31.
 */

public class CollectAdapter extends RecyclerViewAdapter<CollectAdapter.ViewHolder> implements RetrofitListener<Result>{
    private UserModel userModel;
    private String shop_id,food_id;

    public CollectAdapter(Context context, List data, int layoutResource) {
        super(context, data, layoutResource);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final CollectInfo entity = (CollectInfo) data.get(position);
        if (entity.getFlag().equals("0")) {
            holder.collect_shopid.setText(entity.getShop_id());
            holder.collect_foodid.setText(entity.getFood_id());
            holder.collect_name.setText(entity.getShopname());
            holder.collect_content.setText(entity.getAddress());

            holder.collect_enter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, FoodListActivity.class);
                    intent.putExtra("id", entity.getShop_id());
                    intent.putExtra("shopname", entity.getShopname());
                    context.startActivity(intent);
                }
            });

            holder.collect_cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    uncollectShop(entity.getShop_id());
                }
            });
        } else {
            holder.collect_name.setText(entity.getFoodname());
            holder.collect_content.setText(entity.getPrice() + "元");


            holder.collect_enter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, FoodInfoActivity.class);
                    intent.putExtra("id", entity.getFood_id());
                    context.startActivity(intent);
                }
            });

            holder.collect_cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    uncollectFood(entity.getFood_id());
                }
            });
        }
        if (!entity.getPic().equals("")) {
            Picasso.get().load(Constants.BASE_URL+entity.getPic()).into(holder.collect_pic);
        }

    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView collect_pic;
        private TextView collect_name, collect_content, collect_shopid, collect_foodid;
        private Button collect_enter, collect_cancle;

        public ViewHolder(View itemView) {
            super(itemView);
            collect_pic = (ImageView) itemView.findViewById(R.id.collect_pic);
            collect_name = (TextView) itemView.findViewById(R.id.collect_name);
            collect_content = (TextView) itemView.findViewById(R.id.collect_content);
            collect_shopid = (TextView) itemView.findViewById(R.id.collect_shopid);
            collect_foodid = (TextView) itemView.findViewById(R.id.collect_foodid);
            collect_enter = (Button) itemView.findViewById(R.id.collect_enter);
            collect_cancle = (Button) itemView.findViewById(R.id.collect_cancle);
        }
    }

    private void uncollectShop(String Shop_id) {
        this.shop_id=Shop_id;
        userModel = new UserModel();
        userModel.userCollectShop(getUser_id(), Shop_id,this);
    }

    private void uncollectFood(String Food_id) {
        this.food_id=Food_id;
        userModel = new UserModel();
        userModel.userCollectFood(getUser_id(), Food_id,this);
    }

    @Override
    public void onSuccess(Result status, int flag) {
        if(flag== Constants.USERCOLLECTFOOD)
        {
            if (status.getSuccess().equals("1")) {
                Toast.makeText(context, "取消收藏菜品成功", Toast.LENGTH_SHORT).show();
                for (int i = 0; i < data.size(); i++) {
                    CollectInfo object = (CollectInfo) data.get(i);
                    if (object.getFood_id().equals(food_id)) {
                        data.remove(object);
                        notifyDataSetChanged();
                        break;
                    }
                }
            } else {
                Toast.makeText(context, "取消收藏菜品失败", Toast.LENGTH_SHORT).show();
            }

        }else
        {
            if (status.getSuccess().equals("1")) {
                Toast.makeText(context, "取消收藏店铺成功", Toast.LENGTH_SHORT).show();
                for (int i = 0; i < data.size(); i++) {
                    CollectInfo object = (CollectInfo) data.get(i);
                    if (object.getShop_id().equals(shop_id)) {
                        data.remove(object);
                        notifyDataSetChanged();
                        break;
                    }
                }
            } else {
                Toast.makeText(context, "取消收藏店铺失败", Toast.LENGTH_SHORT).show();
            }

        }
    }

    @Override
    public void onFail() {

    }
}
